package com.lcx.plugin.neteaseCloud.component.handle;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import com.lcx.plugin.neteaseCloud.music.api.MusicListApi;
import com.lcx.plugin.neteaseCloud.music.entity.MusicListEntity;

import java.util.Collections;
import java.util.Objects;

/**
 * @author daxiong
 * @date 2021/10/13 下午2:16
 */
public class SongQueueHandle {

    /**
     * 处理歌曲详情点击事件
     *
     * @param selectedIndex
     * @param selectedValue
     * @param musicListEntity
     * @return void
     * @author daxiong
     * @date 2021/10/13 下午2:17
     */
    public static void handle(int selectedIndex, String selectedValue, MusicListEntity musicListEntity) {
        switch (selectedIndex) {
            case 0:
                break;
            case 1:
                // 添加到队列
                addAll2Queue(musicListEntity);
                break;
            default:
                break;
        }
    }

    /**
     * 将歌曲添加到待播队列
     *
     * @param parentEntity
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午4:16
     */
    public static void addAll2Queue(MusicListEntity parentEntity) {
        JSONArray musicListDetail = MusicListApi.getMusicListDetail(parentEntity.getId());
        if (Objects.nonNull(musicListDetail)) {
            for (Object detail : musicListDetail) {
                JSONObject detailObj = (JSONObject) detail;
                MusicListEntity musicEntity = transfer2MusicEntity(parentEntity, detailObj);
                SongQueueHandle.add2Queue(musicEntity, null);
            }
        }
    }

    /**
     * 获取歌单下的所有歌曲实体
     *
     * @param parentEntity 歌单实体
     * @param detail       歌曲的json对象
     * @return void
     * @author daxiong
     * @date 2021/10/17 下午9:20
     */
    public static MusicListEntity transfer2MusicEntity(MusicListEntity parentEntity, JSONObject detail) {
        String songId = detail.getString("id");
        String songName = detail.getString("name");
        // 歌手信息
        JSONArray ar = detail.getJSONArray("ar");
        // 歌曲信息
        JSONObject al = detail.getJSONObject("al");
        // 创建歌曲实体
        MusicListEntity entity = new MusicListEntity(parentEntity.getId(), parentEntity.getName(), 2);
        entity.setSongId(songId);
        entity.setSongName(songName);
        if (ar != null && !ar.isEmpty()) {
            JSONObject arObj = (JSONObject) ar.get(0);
            entity.setAuthorId(arObj.getString("id"));
            entity.setAuthorName(arObj.getString("name"));
        }
        if (al != null) {
            entity.setAlbumName(al.getString("name"));
            entity.setSongAvatar(al.getString("picUrl"));
        }
        return entity;
    }


    /**
     * 将歌曲添加到待播队列
     *
     * @param musicListEntity
     * @param index
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午4:16
     */
    public static void add2Queue(MusicListEntity musicListEntity, Integer index) {
        // 先判断队列中是否已经有该歌曲
        if (DataCenter.queueList.contains(musicListEntity)) {
            if (Objects.isNull(index)) {
                // 已经有了，直接退出，也不需要刷新面板
                return;
            } else {
                // 重新调整位置
                DataCenter.queueList.remove(musicListEntity);
                DataCenter.queueList.add(index, musicListEntity);
            }
        } else {
            // 之前没有，则添加到队列的尾端
            if (Objects.isNull(index)) {
                DataCenter.queueList.add(musicListEntity);
            } else {
                DataCenter.queueList.add(index, musicListEntity);
            }
        }
        // 刷新面板
        DataCenter.mainPanel.setQueuePanel();
        if (Objects.isNull(DataCenter.playingSongEntity)) {
            // 当前没有播放的歌曲，则直接播放
            SongPlayHandle.beginPlaySong(musicListEntity);
        }
    }

    /**
     * 随机打乱播放顺序
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 上午9:56
     */
    public static void randomQueue() {
        if (CollUtil.isNotEmpty(DataCenter.queueList) && DataCenter.queueList.size() > 1) {
            MusicListEntity playingSong = DataCenter.queueList.remove(0);
            Collections.shuffle(DataCenter.queueList);
            DataCenter.queueList.add(0, playingSong);
            DataCenter.mainPanel.setQueuePanel();
        }
    }

    /**
     * 单曲循环
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 上午10:13
     */
    public static void repeat() {
        DataCenter.playMode = 1;
    }

    /**
     * 取消单曲循环
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 上午10:13
     */
    public static void cancelRepeat() {
        DataCenter.playMode = 0;
    }
}
